package com.example.xianji.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.My_LiuYan_Adapter;
import com.example.xianji.BGARefresh.BGARefreshLayout;
import com.example.xianji.BGARefresh.BGARefreshLayout.BGARefreshLayoutDelegate;
import com.example.xianji.bean.My_DuiJie;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Main_My_LiuYanActivity extends Activity implements OnClickListener, BGARefreshLayoutDelegate {
    /*****************************我收到的留言***************************************/
    private RelativeLayout my_liuyan_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private BGARefreshLayout liuyan_RefreshLayout;
    private ListView liuyan_mylistview;
    private ArrayList<My_DuiJie> liuyan_list = new ArrayList<My_DuiJie>();
    private int page = 1;
    private My_LiuYan_Adapter mMy_LiuYan_Adapter;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                liuyan_mylistview.setAdapter(mMy_LiuYan_Adapter);
                liuyan_RefreshLayout.endRefreshing();
            }
            if (msg.what == 2) {
                mMy_LiuYan_Adapter.notifyDataSetChanged();
                liuyan_RefreshLayout.endLoadingMore();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__my__liu_yan);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        //XJApplication.getInstance().addActivity(this);
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                my_liuyan();
            }
        });

        my_liuyan_back = (RelativeLayout) findViewById(R.id.my_liuyan_relative_back);

        liuyan_RefreshLayout = (BGARefreshLayout) findViewById(R.id.liuyan_RefreshLayout);
        liuyan_mylistview = (ListView) findViewById(R.id.liuyan_mylistview);
        mMy_LiuYan_Adapter = new My_LiuYan_Adapter(this, liuyan_list);
        liuyan_RefreshLayout.setDelegate(this);
        my_liuyan_back.setOnClickListener(this);
        processLogic(savedInstanceState);
    }

    protected void processLogic(Bundle savedInstanceState) {
        liuyan_RefreshLayout.setRefreshViewHolder(new com.example.xianji.BGARefresh.BGANormalRefreshViewHolder(this, true));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.my_liuyan_relative_back) {
            this.finish();
        }
    }

    public void my_liuyan() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("page", 1);
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.my_liuyan,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        liuyan_list.clear();
                        JSONArray array = response.getJSONArray("data");
                        if (array.length() > 0) {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject object = array.getJSONObject(i);
                                My_DuiJie mMy_DuiJie = new My_DuiJie();
                                mMy_DuiJie.setTitle(object.getString("detail"));
                                mMy_DuiJie.setCreated_at(object.getString("created_at"));
                                mMy_DuiJie.setDemand_id(object.getString("btob"));
                                liuyan_list.add(mMy_DuiJie);
                            }
                        } else {
                            Toast.makeText(Main_My_LiuYanActivity.this, "没有数据哦", Toast.LENGTH_SHORT).show();
                        }
                    }
                    handler.sendEmptyMessage(1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    public void my_liuyan_more() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("page", page);
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));

        JSONObject params = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.my_liuyan,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        if (array.length() > 0) {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject object = array.getJSONObject(i);
                                My_DuiJie mMy_DuiJie = new My_DuiJie();

                                mMy_DuiJie.setTitle(object.getString("detail"));
                                mMy_DuiJie.setCreated_at(object.getString("created_at"));
                                mMy_DuiJie.setDemand_id(object.getString("btob"));
                                liuyan_list.add(mMy_DuiJie);
                            }
                        } else {
                            Toast.makeText(Main_My_LiuYanActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
                        }
                    }

                    handler.sendEmptyMessage(2);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        page = 1;
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                my_liuyan();
            }
        });
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        page++;
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                my_liuyan_more();
            }
        });
        return true;
    }
}
